package com.zyq.demo.umenglib;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.umeng.message.UmengNotifyClick;
import com.umeng.message.UmengNotifyClickActivity;
import com.umeng.message.entity.UMessage;

import org.android.agoo.common.AgooConstants;

/**
 * 接收离线推送消息
 * @author Zyq
 * @date 2020/11/9　14:40.
 */
public class TestMiPushActivity  extends Activity {
    private static final String TAG = "MfrMessageActivity";

    private TextView tv_body;

    private final UmengNotifyClick mNotificationClick = new UmengNotifyClick() {
        @Override
        public void onMessage(UMessage msg) {
            final String body = msg.getRaw().toString();
            Log.d(TAG, "body: " + body);
            if (!TextUtils.isEmpty(body)) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //获取消息并自行处理
                        ownOnMessage(body);
                    }
                });
            }
        }
    };

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_mipush);
        mNotificationClick.onCreate(this, getIntent());
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mNotificationClick.onNewIntent(intent);
    }


    /**
     * 处理消息内容
     * @param body
     */
    public void ownOnMessage(String body) {
        /**
         * display_type : notification
         * extra : {"url":"https://www.bus365.com"}
         * msg_id : uujua9u157464968593710
         * body : {"after_open":"go_activity","play_lights":"false","ticker":"测试标题","play_vibrate":"false","activity":"cn.nova.phone.ui.UrlLauncherActivity","text":"测试内容","title":"测试标题","play_sound":"true"}
         * random_min : 0
         */
        try {
            //body示例
            if(!TextUtils.isEmpty(body)){
                tv_body.setText(body);
            }
            // UmengMessageBody messageBody 可以通过body解析成UmengMessageBody，完成自定义的一系列动作
//            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
